package org.nbile.component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.stellar.sdk.KeyPair;
import org.stellar.sdk.Memo;
import org.stellar.sdk.Server;
import org.stellar.sdk.SetOptionsOperation;
import org.stellar.sdk.Transaction;
import org.stellar.sdk.responses.AccountResponse;
import org.stellar.sdk.responses.SubmitTransactionResponse;

/**
 * Document Start 
 * 账户操作类
 * Document End 
 * Author: songwenpeng@le.com
 * Time: 2016年9月27日 上午12:13:52
 */
public class AccountComponent {
	
	/**
	 * Document Start 
	 * 创建随机账号
	 * Document End 
	 * Author: songwenpeng@le.com
	 * Time: 2016年10月3日 下午12:22:11
	 * @return
	 */
	public KeyPair createAccount() {
		KeyPair pair = KeyPair.random();
		return pair;
	}

	

	/**
	 * Document Start 
	 * 获取账户明细
	 * 如果账户在网络不存在则返回Null
	 * 否则返回所有资产明细
	 * Document End 
	 * Author: songwenpeng@le.com
	 * Time: 2016年10月3日 下午12:44:46
	 * @param accountID
	 * @return
	 */
	public List<AccountResponse.Balance> accountDetails(Server server,String accountID) {
		KeyPair accountPair = KeyPair.fromAccountId(accountID);
		List<AccountResponse.Balance> result = new ArrayList<AccountResponse.Balance>();
		AccountResponse account;
		try {
			account = server.accounts().account(accountPair);
			//获取所有资产明细
			for (AccountResponse.Balance balance : account.getBalances()) {
				result.add(balance);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return result;
	}

	/**
	 * 根据Seed获取该Seed对应的AccountID
	 * @param seed
	 * @return
	 */
	public String getAccountIDBySeed(String seed){
		KeyPair source = KeyPair.fromSecretSeed(seed);
		return source.getAccountId();
	}

	/**
	 * Document Start 
	 * 设置通胀选票
	 * Document End 
	 * Author: songwenpeng@le.com
	 * Time: 2016年10月6日 下午6:18:43
	 * @param sourceSeed
	 * @param targetAccountID
	 */
	public SubmitTransactionResponse setInflation(Server server,String sourceSeed, String targetAccountID) {
		//创建源 目标 账户对象
		KeyPair source = KeyPair.fromSecretSeed(sourceSeed);
		KeyPair destination = KeyPair.fromAccountId(targetAccountID);
		try {
			//检查目标账户是否存在
			server.accounts().account(destination);

			// If there was no error, load up-to-date information on your account.
			AccountResponse sourceAccount = server.accounts().account(source);

			// CreateOperation 创建一个账户 并设置初始化保证金金额
			Transaction transaction = new Transaction.Builder(sourceAccount).addOperation(new SetOptionsOperation.Builder().setInflationDestination(destination).build())
					// A memo allows you to add your own metadata to a transaction. It's
					// optional and does not affect how Stellar treats the transaction.
					.addMemo(Memo.text("Active Account")).build();
			// Sign the transaction to prove you are actually the person sending it.
			transaction.sign(source);
			// And finally, send it off to Stellar!
			SubmitTransactionResponse response = server.submitTransaction(transaction);
			return response;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Document Start 
	 * 向区块链上添加ManageData
	 * Document End 
	 * Author: songwenpeng@le.com
	 * Time: 2017年3月3日 下午5:34:59
	 * @param server
	 * @param sourceSeed
	 * @param targetAccountID
	 * @return
//	 */
//	public SubmitTransactionResponse addManageData(Server server,String sourceSeed, String targetAccountID) {
//		//创建源 目标 账户对象
//		KeyPair source = KeyPair.fromSecretSeed(sourceSeed);
//		KeyPair destination = KeyPair.fromAccountId(targetAccountID);
//		try {
//			//检查目标账户是否存在
//			server.accounts().account(destination);
//
//			// If there was no error, load up-to-date information on your account.
//			AccountResponse sourceAccount = server.accounts().account(source);
//
//			// CreateOperation 创建一个账户 并设置初始化保证金金额
//			Transaction transaction = new Transaction.Builder(sourceAccount).addOperation(new SetOptionsOperation.Builder().setInflationDestination(destination).build())
//					// A memo allows you to add your own metadata to a transaction. It's
//					// optional and does not affect how Stellar treats the transaction.
//					.addMemo(Memo.text("Active Account")).build();
//			// Sign the transaction to prove you are actually the person sending it.
//			transaction.sign(source);
//			// And finally, send it off to Stellar!
//			SubmitTransactionResponse response = server.submitTransaction(transaction);
//			return response;
//		} catch (Exception e) {
//			e.printStackTrace();
//			return null;
//		}
//	}
}
